<?
/* Copyright (c) 2008-2011 Submit Consulting. Lima-Peru. All Right Reserved.
 * @author Ing. Angel Sullon M. (asullom@hotmail.com)
 * Created: 21.SEP.2000
 * Last Modify: 11.AGO.2011
 * Description of CryptoString: Encripta y Desencripta una cadena
 */
class CryptoString {
    function keyED($txt,$encryptKey)
    {
            $encryptKey = md5($encryptKey);
            $ctr=0;
            $tmp = "";
            for ($i=0;$i<strlen($txt);$i++){
                    if ($ctr==strlen($encryptKey)) $ctr=0;
                    $tmp.= substr($txt,$i,1) ^ substr($encryptKey,$ctr,1);
                    $ctr++;
            }
            return $tmp;
    }

    function encryptCustomer($txt,$key)
    {
            srand((double)microtime()*1000000);
            $encryptKey = md5(rand(0,32000));
            $ctr=0;
            $tmp = "";
            for ($i=0;$i<strlen($txt);$i++)
            {
            if ($ctr==strlen($encryptKey)) $ctr=0;
            $tmp.= substr($encryptKey,$ctr,1) .
            (substr($txt,$i,1) ^ substr($encryptKey,$ctr,1));
            $ctr++;
            }
            return base64_encode($this->keyED($tmp,$key));
    }

    function decryptCustomer($txt,$key)
    {
            $txt = $this->keyED(base64_decode($txt),$key);
            $tmp = "";
            for ($i=0;$i<strlen($txt);$i++){
                    $md5 = substr($txt,$i,1);
                    $i++;
                    $tmp.= (substr($txt,$i,1) ^ $md5);
            }
            return $tmp;
    }

    function randPass()
    {
            $randomPassword = "";
            srand((double)microtime()*1000000);
            for($i=0;$i<8;$i++)
            {
                    $randNumber = rand(48,120);

                    while (($randNumber >= 58 && $randNumber <= 64) || ($randNumber >= 91 && $randNumber <= 96))
                    {
                            $randNumber = rand(48,120);
                    }

                    $randomPassword .= chr($randNumber);
            }
            return $randomPassword;
    }
    
    public function encrypt($txt) {
        $crypto=new CryptoString();
        return $crypto->encryptCustomer($txt,'ACED00057372001E636F6D2E73756E2E63727970746F2E70726F76696465722E4445534B65796B349C35DA1568980200015B00036B65797400025B427870757200025B42ACF317F8060854E00200007870000000086438D0196107EFF1');
    }
    public function decrypt($txt) {
        $crypto=new CryptoString();
        return $crypto->decryptCustomer($txt, 'ACED00057372001E636F6D2E73756E2E63727970746F2E70726F76696465722E4445534B65796B349C35DA1568980200015B00036B65797400025B427870757200025B42ACF317F8060854E00200007870000000086438D0196107EFF1');
    }
}
//$md5= new CryptoString();
//echo CryptoString::encrypt("123");
//echo "<br>";
//echo CryptoString::decrypt("DjZXNwVk");
?>

